package com.gopay.bis.bocom.ebank.operation;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.bankcomm.netpay.Config;
import com.bankcomm.netpay.Post;
import com.gopay.bis.common.util.BankLogUtils;
import com.gopay.bis.common.util.TransUtils;
import com.gopay.common.constants.bank.BocomConstants;
import com.gopay.common.constants.proccode.ProcCodeConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.BocomB2cMessage;
import com.gopay.common.domain.bank.bocom.ebank.pay.BocomB2cQueryDetail;
import com.gopay.common.exception.GopayException;

public class BocomB2cQuery {
    private String filePath;
	
    /**
     * 交行B2C查询
     * 
     * @param BOCOMMessageB2C
     * @return BOCOMMessageB2C
     */
    public BocomB2cMessage query(BaseMessage message) throws GopayException{
    	try{
    	    String configID = "b2cpay";
            // 借贷分离判断
            if (!message.isSupportCredit()) {
                configID = "debitb2cpay";
            }

            Config.loadConfig(filePath);
            String reqSrc= "<?xml version=\"1.0\" encoding=\"gb2312\"?>"+
                    "<Document>"+
                    "<Head>"+
                    "<TranCode>NPAY1011</TranCode>"+
                    "<MerchNo>"+Config.getMerID(configID)+"</MerchNo>"+
                    "</Head>"+
                    "<Body>"+
                    "<OrderNos>"+message.getOrderId()+"</OrderNos>"+
                    "</Body>"+
                    "</Document>";
            BankLogUtils.printB2CQuerySendLog(message, " BOCOM B2C query orderid:"+ reqSrc);
            
            String reqSign = Config.attachedSign(configID, reqSrc);
            String resSign = Post.post(Config.getHttpsType(configID), Config.getApiURL(configID), "reqData="
                    + java.net.URLEncoder.encode(reqSign, "GBK"));
            String resSrc = Config.attachedVerify(configID, resSign);
            BankLogUtils.printB2CQueryBlackLog(message, resSrc);
            
            BocomB2cMessage b2c = new BocomB2cMessage();
            // 增加查询失败判断
            if (resSrc == null) {
                b2c.setErrorMessage(message.getOrderId() + " bocom b2c query return null");
                BankLogUtils.printB2CQueryBlackLog(b2c, ToStringBuilder.reflectionToString(b2c));
                return b2c;
            }

            // 响应状态
            String rspType = "";
            String orderList = "";

            if (!resSrc.contains("<RspType>")) {
                // 对账响应类型为空
                b2c.setRetCode("E");
                b2c.setErrorMessage("bocom b2c query return RspType is null");
                BankLogUtils.printB2CQueryBlackLog(message, ToStringBuilder.reflectionToString(b2c));
                return b2c;
            }

            rspType = TransUtils.getMsgField("<RspType>", "</RspType>", resSrc);
            b2c.setRetCode(rspType);
            if (!BocomConstants.RSPTYPE_SUCCESS.equals(rspType)) {
                b2c.setRetCode("E");
                b2c.setErrorMessage("bocom b2c query return RspType is error");
            } else {
                orderList = TransUtils.getMsgField("<OrderList>", "</OrderList>", resSrc);
            }
            
            if (!"".equals(orderList)) {
                List<BocomB2cQueryDetail> details = new ArrayList<BocomB2cQueryDetail>();
                BocomB2cQueryDetail detail = new BocomB2cQueryDetail();

                detail.setOrder(TransUtils.getMsgField("<OrderNo>", "</OrderNo>", resSrc));
                detail.setAmount(TransUtils.getMsgField("<Amount>", "</Amount>", resSrc));
                detail.setOrderState(TransUtils.getMsgField("<TranState>", "</TranState>", resSrc));
                details.add(detail);
                b2c.setQueryDetails(details);
            }
            BankLogUtils.printB2CQueryBlackLog(message, ToStringBuilder.reflectionToString(b2c));
            return b2c;         
    	}catch(Exception e){
    		BankLogUtils.printB2CQueryExceptionLog(message, e);
    		throw new GopayException(ProcCodeConstants.PROC_CODE_100E6037);
    	}   
    }

    public String getFilePath() {
        return filePath;
    }

    public void setFilePath(String filePath) {
        this.filePath = filePath;
    }
}
